﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework.Audio;

namespace PonGun
{
    public class Button : Base2DObject
    {
        public UIEvent OnMousePointerClicked;

        public string AssetOver { get; set; }
        public string AssetClicked { get; set; }
        public string AssetNormal { get; set; }

        GSM GSM { get; set; }

        SoundEffectInstance clickedSnd;
        SoundEffectInstance hoverSnd;


        public Button(Game1 game, GSM gsm, string assetNormal, string assetOver,string assetClicked)
            : base(game)
        {
            GSM = gsm;

            AssetOver = assetOver;
            AssetNormal = assetNormal;
            AssetClicked = assetClicked;

            Asset = AssetNormal;

            Width = 200;
            Height = 64;
        }

        bool playOver = false;
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (clickedSnd == null)
                clickedSnd = Game.Content.Load<SoundEffect>("Audio/SFX/buttonClick").CreateInstance();

            if (hoverSnd == null)
                hoverSnd = Game.Content.Load<SoundEffect>("Audio/SFX/buttonHover").CreateInstance();

            Asset = AssetNormal;

            if (Bounds.Intersects(GSM.MousePointer.MouseRect))
            {
                Asset = AssetOver;


                if (!playOver && hoverSnd.State != SoundState.Playing)
                    hoverSnd.Play();

                playOver = true;

                if (Game.IsLeftMouseDown())
                {
                    Asset = AssetClicked;
                }

                if (Game.IsLeftMouseClicked() && OnMousePointerClicked != null)
                {
                    if (clickedSnd.State != SoundState.Playing)
                        clickedSnd.Play();

                    OnMousePointerClicked(this);
                }
            }
            else
                playOver = false;

            ReSetTexture2D();

            base.Update(gameTime);
        }

        
    }
}
